package com.example.product.exception;

import com.example.common.exception.ExceptionCodeEnum;
import com.example.common.utils.R;
import com.example.common.utils.ValidateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.Map;

/**
 * 全局异常处理
 */
@Slf4j
@RestControllerAdvice
public class GmallProductGlobalExceptionHandler {

    @ExceptionHandler({MethodArgumentNotValidException.class})
    public R handleValidException(MethodArgumentNotValidException e) {
        return ValidateUtils.handleValidException(e);
    }


    @ExceptionHandler({Exception.class})
    public R handleOtherException(Exception e) {
        log.error("未知系统异常 ==================== {}",e.getMessage());
        return R.error(ExceptionCodeEnum.UNKNOWN_SYSTEM_EXCEPTION.getCode(),ExceptionCodeEnum.UNKNOWN_SYSTEM_EXCEPTION.getMessage());
    }
}
